<?php
/**
 * @copyright Copyright (C) 2010 redCOMPONENT.com. All rights reserved.
 * @license GNU/GPL, see license.txt or http://www.gnu.org/copyleft/gpl.html
 * Developed by email@recomponent.com - redCOMPONENT.com
 *
 * redSHOP can be downloaded from www.redcomponent.com
 * redSHOP is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.
 *
 * You should have received a copy of the GNU General Public License
 * along with redSHOP; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
 * We are in redSHOP Using the Traditional Snippet
 * This document describes how to set up Analytics tracking for your website using the traditional ga.js tracking code snippet.
 * If you are setting up tracking for the first time, we recommend that you use the default tracking code snippet, described in Tracking Sites.
 */
class googleanalytics
{
	var $_data = null;
	var $_table_prefix = null;

	function __construct()
	{
	  	$this->_table_prefix = '#__'.TABLE_PREFIX;
	}

	/**
	 * When you first begin implementing tracking in Google Analytics website, you need to install the tracking code on your website pages.
	 * The generic tracking code snippet consists of two parts: a script tag that references the ga.js tracking code,
	 * and another script that executes the tracking code.
	 */
	function pageTrackerView(){

		# The first line of the tracking script should always initialize the page tracker object.
         $pagecode = "
		try{
		 	var pageTracker = _gat._getTracker('".GOOGLE_ANA_TRACKER_KEY."');
			pageTracker._trackPageview();
			pageTracker._trackPageLoadTime();
		} catch(err) {}
		";

		return $pagecode;
	}

	/**
	 * Creates a transaction object with the given values.
	 * As with _addItem(), this method handles only transaction tracking and provides no additional ecommerce functionality.
	 * Therefore, if the transaction is a duplicate of an existing transaction for that session, the old transaction values are over-written with the new transaction values.
	 * Arguments for this method are matched by position, so be sure to supply all parameters, even if some of them have an empty value.
	 */
	function addTrans($data)
	{
		$packegecode = "
			pageTracker._addTrans(\"".$data['order_id']."\",
								\"".$data['shopname']."\",
								\"".$data['order_total']."\",
								\"".$data['order_tax']."\",
								\"".$data['order_shipping']."\",
								\"\",
								\"".$data['state']."\",
								\"".$data['country']."\"
								);
	    ";
		$doc =& JFactory::getDocument();
		$doc->addScriptDeclaration( $packegecode );

	}

	/**
	 * Use this method to track items purchased by visitors to your ecommerce site.
	 * This method tracks individual items by their SKU.
	 * This means that the sku parameter is required.
	 * This method then associates the item to the parent transaction object via the orderId argument.
	 */
	function addItem($itemdata)
	{
		$itemdata['product_name'] = str_replace("\n"," ",$itemdata['product_name']);
		$itemdata['product_name'] = str_replace("\r"," ",$itemdata['product_name']);

		$packegecode = "
			pageTracker._addItem(\"".$itemdata['order_id']."\",
								\"".$itemdata['product_number']."\",
								\"".$itemdata['product_name']."\",
								\"".$itemdata['product_category']."\",
								\"".$itemdata['product_price']."\",
								\"".$itemdata['product_quantity']."\");
	    ";

		$doc =& JFactory::getDocument();
		$doc->addScriptDeclaration( $packegecode );
	}

	/**
	 * Sends both the transaction and item data to the Google Analytics server.
	 * This method should be called after _trackPageview(), and used in conjunction with the _addItem() and addTrans() methods.
	 * It should be called after items and transaction elements have been set up.
 	 */
	function trackTrans(){

		# submits transaction to the Analytics servers
		$packegecode = "
						pageTracker._trackTrans();
						//alert(pageTracker);
					";
		$doc =& JFactory::getDocument();
		$doc->addScriptDeclaration( $packegecode );
	}

	/**
	 * Code setting for google analytics
	 *
	 * As per ecoomerce tracking API
	 * @source: http://code.google.com/apis/analytics/docs/tracking/gaTrackingEcommerce.html
	 */
	function placeTrans($analyticsData){

		if(isset($analyticsData['addtrans'])){

			$addtrans = $analyticsData['addtrans'];

			# add Transaction/Order to google Analytic
			$this->addTrans($addtrans);

			if(isset($analyticsData['addItem'])){

				$addItem = $analyticsData['addItem'];

				$tItem = count($addItem);

				for($i=0;$i<$tItem;$i++){

					$item = $addItem[$i];
					# add Order Items to google Analytic
					$this->addItem($item);
				}
			}

			# track added order to google analytics
			$this->trackTrans();
		}
	}
}